package com.atguigu.bigdata.chapter07.window;

import com.atguigu.bigdata.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @Author lzc
 * @Date 2022/9/4 8:28
 */
public class Flink05_Window_ProcessFunction_Aggregate {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env
            .socketTextStream("hadoop162", 8888)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String value) throws Exception {
                    String[] data = value.split(",");
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .keyBy(WaterSensor::getId)
            .window(TumblingProcessingTimeWindows.of(Time.seconds(10)))
            .aggregate(new AggregateFunction<WaterSensor, Avg, Double>() {
                
                           // 初始一个累加器
                           // 这个窗口内的第一条元素来的触发一次
                           @Override
                           public Avg createAccumulator() {
                               System.out.println("Flink05_Window_ProcessFunction_Aggregate.createAccumulator");
                               return new Avg();
                           }
                
                           // 真正的累加器方法
                           // 每来一条元素触发一次
                           @Override
                           public Avg add(WaterSensor value, Avg acc) {
                               System.out.println("Flink05_Window_ProcessFunction_Aggregate.add");
                               acc.sum += value.getVc();
                               acc.count++;
                               return acc;
                           }
                
                           // 返回最终的结果
                           // 窗口关闭的时候触发一次
                           @Override
                           public Double getResult(Avg acc) {
                               System.out.println("Flink05_Window_ProcessFunction_Aggregate.getResult");
                               return acc.sum * 1.0 / acc.count;
                           }
                
                           // 合并累加器
                           // 只有窗口是 session窗口的时候才有效,其他窗口不会触发
                           @Override
                           public Avg merge(Avg a, Avg b) {
                               System.out.println("Flink05_Window_ProcessFunction_Aggregate.merge");
                               a.sum += b.sum;
                               a.count += b.count;
                               return a;
                           }
                       }
            )
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static class Avg {
        public Integer sum = 0;
        public Long count = 0L;
    }
}
/*
窗口处理函数:
    
    开窗的目的: 把多个元素放入到窗口内, 然后对这个窗口内的元素做计算, 比如聚合, topN...

增量计算
    简单
        sum
        max min
        maxBy minBy
    
    复杂
        reduce
        
        aggregate


全量计算(窗口关闭的时候才进行开始计算)
    process


 */